Ext.define('AmpleHarvest.views.gsbrowse.main',
{
	extend			: 'Ext.panel.Panel',
	alias			: 'widget.ampleharvest-views-gsbrowse-main',
	width			: '100%',
	requires		:
	[
	 		"AmpleHarvest.views.gsbrowse.browse",
	 		"AmpleHarvest.objects.results.results"
	],
	layout			: 'border',
	border			: 1,
	
	// Member Variables
	queryDictionary	: {},
	queryString		: '',
	obj_config		: {},
	
	listeners		:
	{
		processQuery : function(obj_query)
		{
			this.queryString = '';
			if(obj_query[0].gsbrowseCategory.length > 0)
			{
				this.fireEvent('doQuery', obj_query, this);
//				this.buildQuery(obj_query);
//				this.down('ampleharvest-objects-results-results').loadQuery(this.queryString);
			} else {
				this.down('ampleharvest-objects-results-results').clearQuery();
			}
			
			m_obj_AmpleHarvest_ViewPort.fireEvent(AmpleHarvest.eventHandler.cWindowEvents.C_STR_EVENT_ADD_ACTION,
					'AmpleHarvest.views.gsbrowse.main',
					'BROWSE :'+this.queryString,
					{
			    		obj_query		: obj_query
					}
				);
		},
		
		doQuery	: function(obj_query)
		{
//			console.log('do it');
			this.buildQuery(obj_query);
			this.down('ampleharvest-objects-results-results').loadQuery(this.queryString);
		},
		
		processingStart	: function()
		{
			this.m_obj_browse.loadMask.show();
		},
		
		processingFinished : function()
		{
			this.m_obj_browse.loadMask.hide();
		},
		
		openItem	: function(model, record, index)
		{
			m_obj_AmpleHarvest_ViewPort.fireEvent( AmpleHarvest.eventHandler.cWindowEvents.C_STR_EVENT_APPLICATION_OPEN, 
					{
						m_str_id : record.data.Id
					} );
		}
	},
	
	initComponent	: function()
	{
		console.log(this.queryString);
		this.m_obj_browse = Ext.create(
				"AmpleHarvest.views.gsbrowse.browse",
				{
					region	: 'west',
					flex	: 1,
					arr_query	: this.obj_config.obj_query
				}
		);
		this.m_obj_results = Ext.create(
				"AmpleHarvest.objects.results.results",
				{
					region	: 'center',
					flex	: 3,
					maxLimit	: 25,
					store	: Ext.create('AmpleHarvest.DATA.Store.gsSolr',
			    			{
				    			AHcaller	: this
			    			}),
					columns: [
			               {  header      : 'Name', dataIndex   : 'Name', width: 200, sortable : true,
			                  renderer: function(value, metaData, record) { return "<b>"+value+"</b>"; } },
			                  
			               {  header      : 'Version',    dataIndex   : 'Version', width: 50, sortable : true},
			               {  header      : 'Date Downloaded',    dataIndex   : 'Date Downloaded', sortable : true,
			                  renderer: function(value, metaData, record) { return value; } },
			                  
			               {  header      : 'Date Profiled',    dataIndex   : 'Date Profiled', sortable : true,
			                  renderer: function(value, metaData, record) { return value; } },
			                  
			               {  header      : 'Profiler Version',    dataIndex   : 'Profiler Version', sortable : true},
			               {  header      : 'Source',    dataIndex   : 'Source', sortable : true}
			         ]
				}
		);

		this.items =
		[
				this.m_obj_browse,
				this.m_obj_results
		]
		this.callParent();

		if(Ext.isArray(this.obj_config.obj_query))
		{
//			console.log('inside');
//			this.fireEvent('doQuery', this.obj_config.obj_query, this);
		}
	},
	
	buildQuery	: function(obj)
	{
		for(var a in obj)
		{
			for(var b in obj[a])
			{
				for(var c in obj[a][b])
				{
					if(c != 0)
					{
						this.queryString += ' OR ';
					}
					var str = obj[a][b][c]['level']+':';
					str += '"'+obj[a][b][c]['name']+'"';
					
					if(!obj[a][b][c]['leaf'])
					{
						str = '('+str;
						this.queryString += str;
						this.queryString += ' AND ';
						this.buildQuery(obj[a][b][c]['children']);
						this.queryString += ')';

					} else {
						this.queryString += str;
					}
					console.log('finish round:'+c+':'+this.queryString);
				}
			}
		}
		
	},
});


